const db = require('../db/index');

/**
 * account 
 * email
 * name
 * login_time
 */

// 登录记录
exports.loginLog = (req, res) => {
  let {
    account,
    email,
    name
  } = req.body;
  let login_time = new Date();
  let sql = 'insert into login_log set ?';
  db.query(sql, {
    account,
    email,
    name,
    login_time
  }, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('登录记录失败');
    res.send({
      status: 0,
      message: '登录记录成功'
    })
  })
}

// 获取登录记录列表
exports.getLoginLogList = (req, res) => {
  let sql = 'select * from login_log';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results,
      total: results.length
    })
  })
}

// 分页
exports.getLoginLogListByPage = (req, res) => {
  let {
    pageSize
  } = req.body;
  pageSize = (pageSize - 1) * 10;
  let sql = `select * from login_log order by login_time limit 10 offset ${pageSIze}`;
  db.query(sql, pageSize, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      results,
    })
  })
}

// 清空登录记录
exports.clearLoginLog = (req, res) => {
  let sql = 'truncate table login_log';
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: '清空记录成功'
    })
  })
}